package kr.memo.controller;


import javax.servlet.http.HttpServletRequest;

import kr.memo.dao.MemoDao;
import kr.memo.domain.MemoCommand;
import kr.memo.validator.MemoValidator;
import kr.parking.dao.ParkingDao;
import kr.parking.domain.ParkingCommand;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class MemoInsertController {
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private MemoDao memoDao;
	@Autowired
	private ParkingDao parkingDao;
	
	@RequestMapping(value="insertMemo.do", method=RequestMethod.GET)
	public String form(@RequestParam("p_num")int p_num,
					   HttpServletRequest request){
		
		ParkingCommand parking = parkingDao.selectParking(p_num);
		request.setAttribute("p_name", parking.getP_name());
		return "insertFormMemo";
	}
	@RequestMapping(value="insertMemo.do", method=RequestMethod.POST)
	public String process(@ModelAttribute("memoCommand") MemoCommand memoCommand,
							BindingResult result){
		
		if(log.isDebugEnabled()){
			log.debug("MemoCommand : " + memoCommand);
		}
		
		new MemoValidator().validate(memoCommand, result);
		
		memoDao.insertMemo(memoCommand);
		
		return "redirect:/memoList.do?p_num="+memoCommand.getP_num();
	}
}




